package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @author yzt
 * @create 2021-09-11 17:41
 */
@Controller
public class AdminHandler {

    @Autowired
    private AdminService adminService;

    //登录请求
    @RequestMapping("/admin/do/login")
    public String doLogin(@RequestParam("loginAcct") String loginAcct,
                            @RequestParam("userPswd") String userPswd,
                            HttpSession session){

        Admin admin = adminService.getAdminByLoginAcct(loginAcct,userPswd);

        session.setAttribute("loginAdmin",admin);

        return "redirect:/admin/to/main";
    }

    //退出登录请求
    @RequestMapping("/admin/do/logout")
    public String doLogout(HttpSession session){
        // 强制session失效
        session.invalidate();
        return "redirect:/admin/to/login";
    }

    //请求分页数据
    @RequestMapping("/admin/get/page")
    public String getPageInfo(
                    //如果传来的参数中有，就用传过来的，如果没有就用默认的
                    @RequestParam(value = "keyword",defaultValue = "") String keyword,
                    @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                    @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                    Model model){

        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);

        model.addAttribute("pageInfo",pageInfo);

        return "admin-page";
    }

    //删除数据
    @RequestMapping("/admin/remove")
    public String removeAdmin(
                    @RequestParam("adminId") Integer adminId,
                    @RequestParam("pageNum") Integer pageNum,
                    @RequestParam("keyword") String keyword
    ){

        adminService.removeAdmin(adminId);

        return "redirect:/admin/get/page?pageNum="+pageNum+"&keyword="+keyword;
    }

    //新增数据
    @RequestMapping("/admin/save")
    public String saveAdmin(Admin admin){
        adminService.saveAdmin(admin);
        return "redirect:/admin/get/page?pageNum="+Integer.MAX_VALUE;
    }

    //前往更新页面
    @RequestMapping("/admin/to/edit")
    public String toEdit(@RequestParam("adminId") Integer adminId,Model model){
        Admin admin = adminService.getAdminById(adminId);
        model.addAttribute("admin",admin);
        return "admin-edit";
    }

    //更新数据
    @RequestMapping("/admin/update")
    public String updateAdmin(Admin admin,
                              @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "keyword",defaultValue = "") String keyword){

        adminService.update(admin);
        return "redirect:/admin/get/page?pageNum="+pageNum+"&keyword="+keyword;
    }
}
